//
//  PresentationTool.m
//  JFTwxsd
//
//  Created by Steven on 2018/9/17.
//  Copyright © 2018年 junfeiweiyejiaoyujituan. All rights reserved.
//

#import "PresentationTool.h"
@interface PresentationTool()
@property(nonatomic, strong)UIView *blackView;
@end



@implementation PresentationTool

#pragma mark - lazy
- (UIView *)blackView{
    if (_blackView == nil) {
        _blackView = [UIView new];
        _blackView.frame = self.containerView.bounds;
    }
    return _blackView;
}

#pragma mark - override

- (void)presentationTransitionWillBegin{
    [self.containerView addSubview:self.blackView];
    [UIView animateWithDuration:.6 animations:^{
        self.blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }];
}

- (void)dismissalTransitionWillBegin{
    [UIView animateWithDuration:0.5 animations:^{
        self.blackView.alpha = 0.0;
    }];
}

- (void)dismissalTransitionDidEnd:(BOOL)completed{
    if (completed) {
        [self.blackView removeFromSuperview];
    }
}

@end


@implementation UIViewController (Present)

- (void)presentBottom:(UIViewController *)controller completed:(dispatch_block_t)completed{
    controller.modalPresentationStyle = UIModalPresentationCustom;
    controller.transitioningDelegate = self;
    [self presentViewController:controller animated:YES completion:completed];
}

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    return [[PresentationTool alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
@end



